package com.fairytask.engine.components.tree
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.checkBox.CheckBox;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.managers.BindManager;
	import com.fairytask.managers.ResManager;
	import com.fairytask.resource.StaticIni;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;

	/**
	 * 树项目 
	 * @author Navy
	 */	
	public class TreeItem extends BaseComponent
	{
		public var data			:Array	=	[];
		//属性
		private var _childArr		:Array	=	[];
		public var parentNote		:TreeItem;
		private var _currentIcon	:MovieClip;
		public var rootTree		:Tree;
		private var _itemName		:String;
		private var _isHidden		:Boolean	=	true;
		//数据
		public var id				:int	=	-1;	//父项的序列
		public var isOpen			:Boolean	=	false;
		private var currentHeight	:int= 18; //当前高度
		//显示
		private var _openSkin		:MovieClip;
		private var _closeSkin		:MovieClip;
		private var _childSpace	:Sprite		=	new Sprite();		//Tree子项容器
		private var _iconCkb		:CheckBox	//	伸缩/展开按钮
		private var _nameTxt		:TextField;	//父项名称
		private var	_filter		:GlowFilter;
		private var _timeTxt		:TextField;//时间显示
		private var _timer			:Timer;//计时器
		private var _time			:int;
		private var _icon			:Clip;
		private var _iconTime	:Clip;//时间显示图标
		private var _layOut		:String = "left";
		private var _bgStr			:String = "";
		private var _isRoot		:Boolean;
		
		
		public var hSpacer			:int	=	5
		public var vSpacer			:int	=	1;		//子项间隔
		
		//默认颜色
		private const DEFAULT_COLOR	:int = 0xFFCB4A;//默认颜色
		
		private var _bindManager	:BindManager;
		
		public function TreeItem(nameStr:String,_sk:String,isRoot:Boolean=false,hideCheck:Boolean=false,pIconSrc:String="",time:int=0,timeIcon:String="",rootBgStr:String="",layOut:String="",color:int=DEFAULT_COLOR)
		{
			_bindManager	=	BindManager.instance;
			_filter	=	Filter.COMMON_LABEL_FILTER;
			_isRoot	=	isRoot;
			if(rootBgStr != "")
			{
				_bgStr = rootBgStr;
				var bg:DisplayObject = ResManager.newResClass(_bgStr) as DisplayObject;
				bg.cacheAsBitmap;
				bg.y = 1;
				addChild(bg);
			}
			
			_skin	=	_sk;
			_iconCkb	=	new CheckBox(_skin);
			_iconCkb.x  = 	3;
			_iconCkb.y	=	3;
			if(!hideCheck)
			{
				addChild(_iconCkb);
			}
			if(layOut!="")
			{
				_layOut = layOut;
			}
			
			var tf		:TextFormat	=	new TextFormat();
			tf.leading	=	2;
			tf.letterSpacing	=	1;
			tf.font	=	"Calibri";
			_nameTxt	=	new TextField();
			_nameTxt.defaultTextFormat		=	tf;
			_nameTxt.mouseWheelEnabled		=	false;
			_nameTxt.selectable		=	false;
			_nameTxt.mouseEnabled	=	true;
			_nameTxt.doubleClickEnabled=true;
			_nameTxt.autoSize	=	TextFieldAutoSize.LEFT;
			_nameTxt.textColor	=	color;
			_nameTxt.filters	=	[_filter];
			_nameTxt.height	=	20;
			//_nameTxt.width	=	50;
			_nameTxt.multiline	=	true;
			_nameTxt.htmlText	=	nameStr;
			_nameTxt.x=	20;
			_itemName 	=	nameStr;
			if(_isRoot)
				_nameTxt.htmlText	=	"<font size='13'>"+"<b>"+nameStr+"</b>"+"</font>"
			addChild(_nameTxt);
			
			//有图标
			if(pIconSrc!=""&&null!=pIconSrc)
			{
				_icon	=	new Clip();	
				_icon.source	=	pIconSrc;
				_icon.x	=	25+_nameTxt.width;
				_icon.buttonMode	=	true;
				addChild(_icon);
				_icon.addEventListener(MouseEvent.CLICK, onIconClick);
			}
			
			if(timeIcon!=""&&null!=timeIcon)
			{
				_iconTime = new Clip();
				_iconTime.source = 	timeIcon;
				_iconTime.x = 0;
				_iconTime.y = this.height - 3;
				addChild(_iconTime);
				_time  = time;
				_timer = new Timer(1000,_time);
				_timer.addEventListener(TimerEvent.TIMER,onTimerCount);
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
				_timer.start();
				_timeTxt = new TextField();
				_timeTxt.width = 125;
				_timeTxt.height= 20;
				_timeTxt.mouseEnabled = false;
				timeTxtSetFilter();
				_timeTxt.x = 21;
				_timeTxt.y = _iconTime.y;
				addChild(_timeTxt);
			}
			
			addChild(_childSpace);
			
			addEvent();
		}
		
		/**
		 * 设定固定宽度文字 
		 * @param val
		 * 
		 */
		public function setFixWidth(val:int):void
		{
			_nameTxt.width	=	val;
			_nameTxt.wordWrap	=	true;
		}
		
		private function onTimerCount(evt:TimerEvent):void
		{
			_time--;
			styleChange(_time);
		}
		
		public function onTimerOver(evt:TimerEvent):void
		{
			if(_timer)
			{
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER,onTimerCount);
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
				_timer = null;
			}
		}
		
		private function addEvent():void
		{
			_nameTxt.addEventListener(MouseEvent.CLICK, onItemClick);
			_nameTxt.addEventListener(MouseEvent.DOUBLE_CLICK,onDoubleClick);
			_iconCkb.addEventListener(MouseEvent.CLICK, onCheckBoxClick);
		}
		
		/**
		 * 把后台的数据按照:02:02:59的格式显示
		 * @param sec
		 * 
		 */		
		public function styleChange(secAll:int):void
		{
			var hour:int = secAll/3600;
			var hourStyle:String;
			var minute:int = (secAll - hour*3600)/60;
			var minuteStyle:String;
			var sec:int  = secAll - hour*3600 -60*minute;
			var secStyle:String;
			if(hour<=9)
			{
				hourStyle = "0"+String(hour);
			}
			else
			{
				hourStyle = String(hour);
			}
			
			if(minute<=9)
			{
				minuteStyle = "0"+String(minute); 
			}
			else
			{
				minuteStyle = String(minute);
			}
			
			if(sec<=9)
			{
				secStyle  = "0"+String(sec);
			}
			else
			{	
				secStyle  = String(sec);
			}
			_timeTxt.htmlText = "<font color='#FFCB4A' size=''>" + StaticIni.getSwfLangStr("1000000379") + "</font>" + "<font color='#FFFFFF' size=''>" + hourStyle + " : " + minuteStyle + " : " + secStyle + "</font>";
		}
		
		public function get timeTxt():TextField
		{
			return _timeTxt;
		}
		
		/**
		 * 时间显示txtfiled设置滤镜 
		 * 
		 */		
		private function timeTxtSetFilter():void
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	6;
			glow.quality			= 	BitmapFilterQuality.LOW;
			_timeTxt.filters		=	[glow];
		}
		
		/**
		 * 点击ICON 
		 * @param evt
		 * 
		 */
		private function onIconClick(evt:MouseEvent):void
		{
			if(rootTree!=null)
			{
				rootTree.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ICON,this,data));
			}
		}
		
		public function get itemName():String
		{
			return _childSpace.name;
		}
		
		/**
		 * 
		 * @双击内容
		 * 
		 */		
		private function onDoubleClick(evt:MouseEvent):void
		{
			if(rootTree!=null&&this.parentNote!=null)
			{
				rootTree.dispatchEvent(new TreeEvent(TreeEvent.DOUBLE_CLICK_ITEM,this,data));
			}
		}
		/**
		 * 点击内容 
		 * @param evt
		 */
		private function onItemClick(evt:MouseEvent):void
		{
			if(rootTree!=null)
			{
				rootTree.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ITEM,this,data));
				rootTree.dispatchEvent(new TextEvent(TextEvent.LINK));
			}
		}
		
		/**
		 * 点击图标
		 * @param evt
		 */
		//TODO CheckBox的效果
		private function onCheckBoxClick(evt:MouseEvent):void
		{
			if(_iconCkb.selected)
			{
				addChild(_childSpace);		//添加Tree子项
				isOpen			=	true;
			}
			else
			{
				if(_childSpace.parent!=null)
				{
					removeChild(_childSpace);
				}
				isOpen			=	false;
			}
		
			
			if(rootTree!=null)
			{
				rootTree.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ICON,this,data));
			}
		}
		
		/**
		 * 添加树子项 
		 * @param str 名称
		 * @param rt 树根
		 * @param dt 数据
		 */
		public function addItem(str:String,rt:Tree,_id:int,dt:Array,hideCheck:Boolean=true,pIconStr:String="",
								time:int=0,timeIcon:String="",color:int=DEFAULT_COLOR,canWrap:Boolean=false,pw:int=0):void
		{
			rootTree	= rt;
//			_nameTxt.x	=	20;
			isOpen	=	true;
			_iconCkb.selected	=	true;
			var tmpNote:TreeItem	=	new TreeItem(str,_skin,false,hideCheck,pIconStr,time,timeIcon,"","",color);
			tmpNote.id	=	_id;
			tmpNote.data	=	dt;
			tmpNote.rootTree	=	rt;
			tmpNote.parentNote	=	this;
			if(canWrap)
			{
				tmpNote.setFixWidth(pw);
			}
			
			_childArr.push(tmpNote);
			tmpNote.y	=	currentHeight;
			if(_layOut == "center")
			{
				tmpNote.nameTxt.x = (tmpNote.rootTree.width - tmpNote.nameTxt.width)/2;
			}
			_childSpace.addChild(tmpNote);
			
			currentHeight	+=	tmpNote.height	+	vSpacer;
		}
		/**
		 * 取得数据 
		 * @return 
		 * 
		 */
		public function get childArr():Array
		{
			return _childArr;
		}
		/**
		 * 移除树项 
		 * @param item
		 * 
		 */
		public function removeItem(item:TreeItem):void
		{
			if(item.parent!=null)
			{
				_childArr.splice(_childArr.indexOf(item),1);
				//没有子项时
				if(_childArr.length==0)
				{
					_iconCkb.selected	=	false;	
				}
				formatItem();
			}
		}
		
		/**
		 * 重新排版树内容 
		 */
		public function formatItem():void
		{
			while(_childSpace.numChildren>0)
			{
				_childSpace.removeChildAt(0);
			}
			currentHeight	=	18;
			
			for(var i:int=0;i<_childArr.length;i++)
			{
				_childSpace.addChild(_childArr[i]);
				_childArr[i].y = currentHeight;
				currentHeight	+=	_childArr[i].height
			}	
		}
		
		/**
		 * 检测是否有这个项 
		 * @param item
		 * @return 
		 * 
		 */
		public function hasItem(item:TreeItem):Boolean
		{
			return _childSpace.contains(item);
		}
		/**
		 * 根据ID取得树项 
		 * @param _id
		 * @return 
		 * 
		 */
		public function getItemById(_id:int):TreeItem
		{
			for each(var item:TreeItem in _childArr)
			{
				if(item.id	== _id)
				{
					return item;					
				}
			}
			return null;
		}
		/**
		 * 根据ID删除树内容 
		 * @param _id
		 * 
		 */
		public function removeItemById(_id:int):void
		{
			var tmpItem:TreeItem	=	getItemById(_id);
			if(tmpItem!=null)
			{
				removeItem(tmpItem);
			}
		}
		
		
		public function set htmlText(value:String):void
		{
			_nameTxt.htmlText	=	value;			
		}
		
		public function get htmlText():String
		{
			return _nameTxt.htmlText; 
		}
		
		public function get nameTxt():TextField
		{
			return _nameTxt;
		}

		public function get isHidden():Boolean
		{
			return _isHidden;
		}

		public function set isHidden(value:Boolean):void
		{
			_isHidden = value;
		}

		
	}
}